﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using DDD.Core;
using DDD.OpenTK;
using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.OpenTK.TestFrameBuffer {
    [TestClass]
    public class TestClearColor_Depth_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion

        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 1, 1);
            };

        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (1, 1, 0);
            var color0 = new Image2D<float> (PixelFormat.RGBA32f, 1, 1);
            var depth = new Image2D<float> (PixelFormat.Depth, 1, 1);
            color0.SetClearColor (new float[4] { 1, 2, 3, 4 });
            depth.SetClearColor (new float[1] { 0.5f });
            
            fbuf.SetColorBuffer (0, color0);
            fbuf.SetDepthBuffer (depth);
            fbuf.SetClear (true, true, true);
            return fbuf;
        }


        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        #endregion



        #region TestMethod
        [TestMethod]
        public void Test_ClearColor_Depth () {

            g3d.Render ();
            window.SwapBuffers ();

            var expected0 = new float[4] { 1, 2, 3, 4 };
            var expected1 = new float[1] { 0.5f };
            var colors0 = new float[4];
            var depths = new float[1];

            var color0 = fbuf.GetColorBuffer (0) as Image2D<float>;
            var depth = fbuf.GetDepthBuffer () as Image2D<float>;

            color0.Capture ();
            depth.Capture ();
            color0.Get (0, 0, 1, 1, colors0);
            depth.Get (0, 0, 1, 1, depths);

            CollectionAssert.AreEqual (expected0, colors0, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected1, depths, new FloatComparer (0.01f));
        }
        #endregion



    }
}
